package stark.app.function.docviewer;

import android.app.Activity;
import android.content.Intent;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.stark.doc.selector.DefDocSelActivity;
import com.stark.docviewer.lib.DocViewUtil;

import java.util.ArrayList;

import stark.app.R;
import stark.app.databinding.FragmentDocviewerDemoBinding;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.constant.Extra;
import stark.common.basic.media.MediaInfo;

/**
 * Author：daizhongbin on 2024/6/4
 * Email：1027154338@qq.com
 * Desc:
 */
public class DocViewerDemoFragment extends BaseNoModelFragment<FragmentDocviewerDemoBinding> {
    @Override
    protected int onCreate() {
        return R.layout.fragment_docviewer_demo;
    }

    @Override
    protected void initView() {
        mDataBinding.btnDocViewer.setOnClickListener(v -> {
            selDoc();
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != Activity.RESULT_OK) return;
        if (requestCode == REQ_SEL_FILE) {
            if (data == null) return;
            MediaInfo mediaInfo = (MediaInfo) data.getSerializableExtra(Extra.DATA);
            DocViewUtil.showWithFileUri((AppCompatActivity) getActivity(), mediaInfo.getUri());
        }
    }

    private static final int REQ_SEL_FILE = 1;

    private void selDoc() {
        ArrayList<String> mimeTypeList = new ArrayList<>();
        mimeTypeList.add("application/pdf");
        mimeTypeList.add("application/msword");
        mimeTypeList.add("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        mimeTypeList.add("application/vnd.ms-powerpoint");
        mimeTypeList.add("application/vnd.openxmlformats-officedocument.presentationml.presentation");
        DefDocSelActivity.selFile(this, mimeTypeList, REQ_SEL_FILE);
    }
}
